package com.ruoyi.basebill.service.impl;

import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsPurinwarehsbillentry;
import com.ruoyi.basebill.mapper.OmsPurinwarehsbillMapper;
import com.ruoyi.basebill.domain.OmsPurinwarehsbill;
import com.ruoyi.basebill.service.IOmsPurinwarehsbillService;

/**
 * 采购入库单Service业务层处理
 * 
 * @author wenyixiong
 * @date 2023-02-15
 */
@Service
public class OmsPurinwarehsbillServiceImpl implements IOmsPurinwarehsbillService 
{
    @Autowired
    private OmsPurinwarehsbillMapper omsPurinwarehsbillMapper;

    @Autowired
    private OmsPurinwarehsbillServiceImpl purinwarehsbillService;

    /**
     * 查询采购入库单
     * 
     * @param id 采购入库单主键
     * @return 采购入库单
     */
    @Override
    public OmsPurinwarehsbill selectOmsPurinwarehsbillById(Long id)
    {
        return omsPurinwarehsbillMapper.selectOmsPurinwarehsbillById(id);
    }

    @Override
    public List<OmsPurinwarehsbill> selectOmsPurinwarehsbillByIds(HashSet<Long> ids)
    {
        return omsPurinwarehsbillMapper.selectOmsPurinwarehsbillByIds(ids);
    }

    /**
     * 查询采购入库单列表
     * 
     * @param omsPurinwarehsbill 采购入库单
     * @return 采购入库单
     */
    @Override
    public List<OmsPurinwarehsbill> selectOmsPurinwarehsbillList(OmsPurinwarehsbill omsPurinwarehsbill)
    {
        return omsPurinwarehsbillMapper.selectOmsPurinwarehsbillList(omsPurinwarehsbill);
    }

    @Override
    public List<OmsPurinwarehsbill> selectOmsPurinwarehsbillDetailList(OmsPurinwarehsbill omsPurinwarehsbill) {
        return omsPurinwarehsbillMapper.selectOmsPurinwarehsbillDetailList(omsPurinwarehsbill);
    }

    /**
     * 新增采购入库单
     * 
     * @param omsPurinwarehsbill 采购入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsPurinwarehsbill(OmsPurinwarehsbill omsPurinwarehsbill)
    {
        int rows = omsPurinwarehsbillMapper.insertOmsPurinwarehsbill(omsPurinwarehsbill);
        purinwarehsbillService.insertOmsPurinwarehsbillentry(omsPurinwarehsbill);
        return rows;
    }

    /**
     * 修改采购入库单
     * 
     * @param omsPurinwarehsbill 采购入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsPurinwarehsbill(OmsPurinwarehsbill omsPurinwarehsbill)
    {
        if (omsPurinwarehsbill.getOmsPurinwarehsbillentryList() != null && omsPurinwarehsbill.getOmsPurinwarehsbillentryList().size() != 0){
            omsPurinwarehsbillMapper.deleteOmsPurinwarehsbillentryByParentid(omsPurinwarehsbill.getId());
            purinwarehsbillService.insertOmsPurinwarehsbillentry(omsPurinwarehsbill);
        }
        return omsPurinwarehsbillMapper.updateOmsPurinwarehsbill(omsPurinwarehsbill);
    }

    /**
     * 批量删除采购入库单
     * 
     * @param ids 需要删除的采购入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsPurinwarehsbillByIds(Long[] ids)
    {
        omsPurinwarehsbillMapper.deleteOmsPurinwarehsbillentryByParentids(ids);
        return omsPurinwarehsbillMapper.deleteOmsPurinwarehsbillByIds(ids);
    }

    /**
     * 删除采购入库单信息
     * 
     * @param id 采购入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsPurinwarehsbillById(Long id)
    {
        omsPurinwarehsbillMapper.deleteOmsPurinwarehsbillentryByParentid(id);
        return omsPurinwarehsbillMapper.deleteOmsPurinwarehsbillById(id);
    }

    /**
     * 新增采购入库单明细信息
     * 
     * @param omsPurinwarehsbill 采购入库单对象
     */
    public void insertOmsPurinwarehsbillentry(OmsPurinwarehsbill omsPurinwarehsbill)
    {
        List<OmsPurinwarehsbillentry> omsPurinwarehsbillentryList = omsPurinwarehsbill.getOmsPurinwarehsbillentryList();
        Long id = omsPurinwarehsbill.getId();
        if (StringUtils.isNotNull(omsPurinwarehsbillentryList))
        {
            List<OmsPurinwarehsbillentry> list = new ArrayList<OmsPurinwarehsbillentry>();
            for (OmsPurinwarehsbillentry omsPurinwarehsbillentry : omsPurinwarehsbillentryList)
            {
                omsPurinwarehsbillentry.setParentid(id);
                list.add(omsPurinwarehsbillentry);
            }
            if (list.size() > 0)
            {
                omsPurinwarehsbillMapper.batchOmsPurinwarehsbillentry(list);
            }
        }
    }

    @Override
    public List<OmsPurinwarehsbill> selectSumBillNumberIsNull() {
        return omsPurinwarehsbillMapper.selectSumBillNumberIsNull();
    }

    @Override
    public int updateSumBillNumberByIds(String billNumber, ArrayList<Long> ids) {
        return omsPurinwarehsbillMapper.updateSumBillNumberByIds(billNumber, ids);
    }
}
